package me.buick.util.jmeter.snmpprocess.gui;

import static me.buick.util.jmeter.snmpprocess.SNMPProcessSamplerConstants.PORT;
import static me.buick.util.jmeter.snmpprocess.SNMPProcessSamplerConstants.SERVER;
import static me.buick.util.jmeter.snmpprocess.SNMPProcessSamplerConstants.SERVER_SUPPORT;
import static me.buick.util.jmeter.snmpprocess.SNMPProcessSamplerConstants.SERVER_TYPE;
import static me.buick.util.jmeter.snmpprocess.SNMPProcessSamplerConstants.SNMP_DATA_LOG_MODE;
import static me.buick.util.jmeter.snmpprocess.SNMPProcessSamplerConstants.SNMP_DATA_LOG_PATH;
import static me.buick.util.jmeter.snmpprocess.SNMPProcessSamplerConstants.SNMP_DATA_RETRIEVE_INTL;
import static me.buick.util.jmeter.snmpprocess.SNMPProcessSamplerConstants.SNMP_PROC_DESCR;
import static me.buick.util.jmeter.snmpprocess.SNMPProcessSamplerConstants.SNMP_SUITE_ID;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import me.buick.util.jmeter.snmpprocess.sampler.SNMPProcessSampler;

import org.apache.jmeter.gui.util.FilePanel;
import org.apache.jmeter.gui.util.VerticalPanel;
import org.apache.jmeter.samplers.gui.AbstractSamplerGui;
import org.apache.jmeter.testelement.TestElement;
import org.apache.jmeter.util.JMeterUtils;

/**
 * 
 * Version 1.0
 * 
 * @author buick
 *
 */

public class SNMPProcessSamplerGui extends AbstractSamplerGui {
	
	private static final long serialVersionUID = 8730735072834496153L;
	
	JTextField server;
	JTextField port;
	JTextField snmpSuiteID;
	JTextField procDescr;
	JTextField snmpDataRetriveInterval;
	JComboBox  supportedServer = new JComboBox();
	JCheckBox saveTo;
    boolean isStats = false;
    JCheckBox loadFrom;
    FilePanel filePanel;
	
	public SNMPProcessSamplerGui() {
		init();
	}

	public String getStaticLabel(){
		return "SNMP Process Sampler";
	}

	public void configure(TestElement element) {
		server.setText(element.getPropertyAsString(SERVER));
		port.setText(element.getPropertyAsString(PORT));
		snmpSuiteID.setText(element.getPropertyAsString(SNMP_SUITE_ID));
		supportedServer.setSelectedItem(element.getPropertyAsString(SERVER_TYPE));
		procDescr.setText(element.getPropertyAsString(SNMP_PROC_DESCR));
		snmpDataRetriveInterval.setText(element.getPropertyAsString(SNMP_DATA_RETRIEVE_INTL));
		String snmpDataLogMode = element.getPropertyAsString(SNMP_DATA_LOG_MODE,"NONE");
		if(snmpDataLogMode.equalsIgnoreCase("LOAD")){
			loadFrom.setSelected(true);
			saveTo.setSelected(false);
		}else if(snmpDataLogMode.equalsIgnoreCase("SAVE")){
			loadFrom.setSelected(false);
			saveTo.setSelected(true);
		}else{
			loadFrom.setSelected(false);
			saveTo.setSelected(false);
		}
		String snmpDataLogPath = element.getPropertyAsString(SNMP_DATA_LOG_PATH);
		if(snmpDataLogPath != null && snmpDataLogPath.trim().length() > 0){
			filePanel.setFilename(snmpDataLogPath);
		}
		super.configure(element);
	}

	public TestElement createTestElement(){
		SNMPProcessSampler sampler = new SNMPProcessSampler();
		modifyTestElement(sampler);
		return sampler;
	}

	public void modifyTestElement(TestElement te){
		te.clear();
		configureTestElement(te);
		te.setProperty(SERVER, server.getText());
		te.setProperty(PORT, port.getText());
		te.setProperty(SNMP_SUITE_ID, snmpSuiteID.getText());
		te.setProperty(SERVER_TYPE, supportedServer.getSelectedItem().toString());
		te.setProperty(SNMP_PROC_DESCR,procDescr.getText());
		te.setProperty(SNMP_DATA_RETRIEVE_INTL,snmpDataRetriveInterval.getText());
		te.setProperty(SNMP_DATA_LOG_PATH, filePanel.getFilename());
		boolean isLoad = loadFrom.isSelected();
		boolean isSave = saveTo.isSelected();
		if(isLoad){
			te.setProperty(SNMP_DATA_LOG_MODE, "LOAD");
		}else if(isSave){
			te.setProperty(SNMP_DATA_LOG_MODE, "SAVE");
		}else{
			te.setProperty(SNMP_DATA_LOG_MODE, "NONE");
		}
		
	}

	void init(){
		setLayout(new BorderLayout(0, 5));
		setBorder(makeBorder());
		add(makeTitlePanel(), BorderLayout.NORTH); // Add the standard title
		VerticalPanel mainPanel = new VerticalPanel();
		mainPanel.add(createServerPanel());
		mainPanel.add(createPortPanel());
		mainPanel.add(createServerSupportPanel());
		mainPanel.add(createSNMPSuiteIDPanel());
		mainPanel.add(createProcessesPanel());
		mainPanel.add(createSNMPDataRetrieveInterval());
		mainPanel.add(createFilePanel());
		add(mainPanel, BorderLayout.CENTER);
	}

	Component createServerPanel() {
		JLabel label = new JLabel(JMeterUtils.getResString("server"));
	
		server = new JTextField(10);
		server.setName(SERVER);
		label.setLabelFor(server);
	
		JPanel serverPanel = new JPanel(new BorderLayout(5, 0));
		serverPanel.add(label, BorderLayout.WEST);
		serverPanel.add(server, BorderLayout.CENTER);
	
		return serverPanel;
	}

	Component createPortPanel() {
		JLabel label = new JLabel("SNMP Port:");
	
		port = new JTextField(10);
		port.setName(PORT);
		label.setLabelFor(port);
	
		JPanel portPanel = new JPanel(new BorderLayout(5, 0));
		portPanel.add(label, BorderLayout.WEST);
		portPanel.add(port, BorderLayout.CENTER);
	
		return portPanel;
	}
	
	Component createServerSupportPanel(){
		JLabel label = new JLabel("Server Type:");
		
		supportedServer = new JComboBox();
		supportedServer.setName(SERVER_TYPE);
		label.setLabelFor(supportedServer);
		
		for(String server:SERVER_SUPPORT)
			supportedServer.addItem(server);
		
		JPanel supportedServerPanel = new JPanel(new BorderLayout(5, 0));
		supportedServerPanel.add(label, BorderLayout.WEST);
		supportedServerPanel.add(supportedServer, BorderLayout.CENTER);
		
		return supportedServerPanel;
		
	}
	
	Component createSNMPSuiteIDPanel() {
		JLabel label = new JLabel("SNMP Suite ID:"); //$NON-NLS-1$
	
		snmpSuiteID = new JTextField(50);
		snmpSuiteID.setName(SNMP_SUITE_ID);
		label.setLabelFor(snmpSuiteID);
	
		JPanel snmpSuiteIDPanel = new JPanel(new BorderLayout(5, 0));
		snmpSuiteIDPanel.add(label, BorderLayout.WEST);
		snmpSuiteIDPanel.add(snmpSuiteID, BorderLayout.CENTER);
	
		return snmpSuiteIDPanel;
	}
	
	Component createProcessesPanel() {
		JLabel label = new JLabel("Processes & Descr:"); //$NON-NLS-1$
	
		procDescr = new JTextField(50);
		procDescr.setName(SNMP_PROC_DESCR);
		label.setLabelFor(procDescr);
	
		JPanel procPanel = new JPanel(new BorderLayout(5, 0));
		procPanel.add(label, BorderLayout.WEST);
		procPanel.add(procDescr, BorderLayout.CENTER);
	
		return procPanel;
	}
	
	Component createSNMPDataRetrieveInterval() {
		JLabel label = new JLabel("SNMP Date Retrieve Interval:"); //$NON-NLS-1$
	
		snmpDataRetriveInterval = new JTextField(50);
		snmpDataRetriveInterval.setName(SNMP_DATA_RETRIEVE_INTL);
		label.setLabelFor(snmpDataRetriveInterval);
	
		JPanel snmpSuiteIDPanel = new JPanel(new BorderLayout(5, 0));
		snmpSuiteIDPanel.add(label, BorderLayout.WEST);
		snmpSuiteIDPanel.add(snmpDataRetriveInterval, BorderLayout.CENTER);
	
		return snmpSuiteIDPanel;
	}
	
	Component createFilePanel() {
		     
		loadFrom = new JCheckBox("Load Perf Log"); // $NON-NLS-1$
		loadFrom.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				if (loadFrom.isSelected()) {
					saveTo.setSelected(false);
				}
			}
		});
		saveTo = new JCheckBox("Save Perf Log"); // $NON-NLS-1$
		saveTo.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				if (saveTo.isSelected()) {
					loadFrom.setSelected(false);
				}
			}
		});
		filePanel = new FilePanel("Save / Load activity data to / from file ", ".xml"); // $NON-NLS-1$ $NON-NLS-2$
		filePanel.add(loadFrom);
		filePanel.add(saveTo);
		return filePanel;
	}

	public void clearGui() {
		super.clearGui();
		server.setText(""); 
		port.setText("");
		snmpSuiteID.setText("");
		supportedServer.setSelectedItem(SERVER_SUPPORT[0]);
		snmpDataRetriveInterval.setText("");
		procDescr.setText("");
		filePanel.setFilename("");
		saveTo.setSelected(false);
		loadFrom.setSelected(false);
	}

	public String getLabelResource() {
		return null;
	}
	
}
